<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-global"></i>
          用户地址
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input
          v-model="search.mobile"
          placeholder="用户手机号"
          class="handle-input mr10"
          width="55"
        ></el-input>
        <el-input
          v-model="search.userMobile"
          placeholder="经营人手机号"
          class="handle-input mr10"
          width="55"
        ></el-input>
        <el-input
          v-model="search.userName"
          placeholder="经营人姓名"
          class="handle-input mr10"
          width="55"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >搜索</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column prop="classifyPic" label="门店大图">
          <template #default="scope">
            <el-image
              class="table-td-thumb"
              :src="pic(scope.row.storeLarge)"
              @click="picPreview(scope.row.storeLarge)"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="classifyPic" label="营业执照正面照片">
          <template #default="scope">
            <el-image
              class="table-td-thumb"
              :src="pic(scope.row.businessLicenseFront)"
              @click="picPreview(scope.row.businessLicenseFront)"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="deposit" label="保证金"></el-table-column>
        <el-table-column prop="accountName" label="账号"></el-table-column>
        <el-table-column prop="merchantName" label="商户名称"></el-table-column>
        <el-table-column
          prop="userMobile"
          label="经营人手机号"
        ></el-table-column>
        <el-table-column prop="userName" label="经营人姓名"></el-table-column>
        <el-table-column
          prop="createTime"
          label="申请时间"
          :formatter="dateFormat"
        ></el-table-column>
        <el-table-column label="是否推荐">
          <template #default="scope">
            <el-tag v-if="scope.row.isRecommend == '0'">否</el-tag>
            <el-tag v-else>推荐</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否上架">
          <template #default="scope">
            <el-tag v-if="scope.row.shelvesStatus == '0'">下架</el-tag>
            <el-tag v-else>上架</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button type="text" @click="update(scope.row)"
              >修改门店信息</el-button
            >
            <el-button
              type="text"
              v-if="scope.row.shelvesStatus == 0"
              @click="updateStatus(scope.row)"
              >上架</el-button
            >
            <el-button
              type="text"
              v-if="scope.row.shelvesStatus == 1"
              @click="updateStatus(scope.row)"
              >下架</el-button
            >
            <el-button
              type="text"
              v-if="scope.row.isRecommend == 0"
              @click="updateIsRecommend(scope.row)"
              >推荐门店</el-button
            >
            <el-button
              type="text"
              v-if="scope.row.isRecommend == 1"
              @click="updateIsRecommend(scope.row)"
              >取消门店推荐</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="deletes(scope.row)"
              >删除门店</el-button
            >
            <el-button type="text" @click="handleUploadCommodity(scope.row)">
              上传商品
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="" v-model="dialogVisible" width="65%">
      <img class="bigImage" :src="pic(dialogImageUrl)" alt="" />
    </el-dialog>

    <!-- 编辑弹出框 -->
    <el-dialog title="创建用户" v-model="showAddUser" width="40%">
      <el-form ref="form" :model="from" label-width="100px">
        <el-form-item label="分类名称">
          <el-input v-model="from.merchantName"></el-input>
        </el-form-item>
        <el-form-item label="经营人手机号">
          <el-input v-model="from.userMobile"></el-input>
        </el-form-item>
        <el-form-item label="经营人姓名">
          <el-input v-model="from.userName"></el-input>
        </el-form-item>
        <el-form-item label="结算比率">
          <el-input v-model="from.settleRatio"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showAddUser = false">取 消</el-button>
          <el-button type="primary" @click="editBdm">确 定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 商品上传对话框 -->
    <CommodityDialog
      v-model:visible="showCommodityDialog"
      :form-data="commodityFormData"
      :is-edit="false"
      :partition-list="partitionList"
      :classify-list="classifyList"
      :disabled="false"
      :merchant-id="currentMerchantId"
      @confirm="handleCommodityConfirm"
    />
  </div>
</template>

<script>
import { api } from "../../api/index";
import { formatDate } from "../../common/data";
import { commodityAdd } from "../../api/zyMall";
import CommodityDialog from "../zyMall/components/CommodityDialog.vue";
import moment from "moment";
export default {
  inject: ["reload"],
  name: "basetable",
  components: { CommodityDialog },
  filters: {
    formatDate(time) {
      time = time * 1000;
      let date = new Date(time);
      console.log(new Date(time));
      return formatDate(date, "yyyy-MM-dd hh:mm");
    },
  },
  data() {
    return {
      query: {
        pageNo: 1,
      },
      search: {},
      tableData: [],
      // multipleSelection: [],
      showAddUser: false,
      pageTotal: 0,
      list: {},
      isChange: "",
      selectedOptions: [],
      dialogImageUrl: "",
      dialogVisible: false,
      from: {},
      // 商品对话框相关
      showCommodityDialog: false,
      commodityFormData: {},
      currentMerchantId: "",
      partitionList: [],
      classifyList: [],
    };
  },
  created() {
    this.getData();
    this.getFilter();
  },
  mounted() {},
  methods: {
    // 获取分区和分类列表
    getFilter() {
      //获取分类列表
      api("/web/scc/pd", { classifyPid: 0 }).then((res) => {
        this.classifyList = res.data;
      });
      //获取分区列表
      api("/web/sep/sl", {}).then((res) => {
        this.partitionList = res.data;
      });
    },
    dateFormat: function (row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    pic(item) {
      return `https://mall.xiyibaomall.com/imgs/${item}`;
    },
    //预览大图
    picPreview(file) {
      this.dialogImageUrl = file;
      this.dialogVisible = true;
    },

    // 获取分类列表
    getData() {
      api("/web/bdm/lists", {
        mobile: this.search.mobile,
        limit: 10,
        offset: this.query.pageNo,
      }).then((res) => {
        // console.log(res.data);
        this.tableData = res.data.rows;
        this.pageTotal = res.data.total;
      });
    },

    // 触发搜索按钮
    handleSearch() {
      this.getData();
    },
    update(row) {
      this.showAddUser = true;
      this.from = JSON.parse(JSON.stringify(row));
      console.log(JSON.parse(JSON.stringify(row)));
    },

    editBdm() {
      this.showAddUser = false;
      api("/web/bdm/update", this.from).then((res) => {
        if (res.code === 2000) {
          this.$message.success("修改成功");
          this.from = {};
          this.reload();
        } else {
          this.$message.error(res.message);
        }
      });
    },

    // 打开上传商品对话框
    handleUploadCommodity(row) {
      this.currentMerchantId = row.id;
      this.commodityFormData = {};
      this.showCommodityDialog = true;
    },

    // 处理商品提交
    handleCommodityConfirm(formData) {
      const data = {
        ...formData,
        commodityClassifyLevel2: 0,
      };

      commodityAdd(data)
        .then((res) => {
          if (res.code === 2000) {
            this.$message.success("上传商品成功");
            this.showCommodityDialog = false;
            // 使用 nextTick 确保对话框关闭后再清空数据
            this.$nextTick(() => {
              this.commodityFormData = {};
              this.currentMerchantId = "";
            });
          } else {
            this.$message.error(res.message);
          }
        })
        .catch(() => {
          this.$message.error("操作失败，请重试");
        });
    },

    // 编辑操作
    updateStatus(row) {
      let shelvesStatus, msg;
      if (row.shelvesStatus == 0) {
        shelvesStatus = 1;
        msg = "确认将该门店上架";
      } else {
        shelvesStatus = 0;
        msg = "确认下架该门店";
      }
      this.$alert(msg, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          api("/web/bdm/updateShelvesStatus", {
            id: row.id,
            shelvesStatus: shelvesStatus,
          }).then((res) => {
            if (res.code === 2000) {
              this.$message.success(res.message);
              this.reload();
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    updateIsRecommend(row) {
      let isRecommend, msg;
      if (row.isRecommend == 0) {
        isRecommend = 1;
        msg = "确认将该门店设置为推荐门店";
      } else {
        isRecommend = 0;
        msg = "确认取消该门店的推荐";
      }
      this.$alert(msg, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          api("/web/bdm/updateIsRecommend", {
            id: row.id,
            isRecommend: isRecommend,
          }).then((res) => {
            if (res.code === 2000) {
              this.$message.success(res.message);
              this.reload();
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    deletes(row) {
      this.$alert("确认删除该门店", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          api("/web/bdm/updateIsDelete", {
            id: row.id,
            isDelete: 1,
          }).then((res) => {
            if (res.code === 2000) {
              this.$message.success(res.message);
              this.reload();
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    // 分页导航
    handlePageChange(val) {
      // console.log(val);
      this.query.pageNo = val;
      this.getData();
    },
  },
};
</script>

<style lang="scss" scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}

.crop-demo {
  display: flex;
  align-items: flex-end;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  width: 178px;
  height: 178px;

  .pre-img {
    width: 178px;
    height: 178px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 5px;
    object-fit: cover;
  }

  .crop-input {
    position: absolute;
    width: 178px;
    height: 178px;
    left: 0px;
    top: 0px;
    opacity: 0;
    cursor: pointer;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
}

.bigImage {
  width: 100%;
}
</style>
